# from twisted.enterprise import adbapi

# dbpool = adbapi.ConnectionPool(
#     "MySQLdb",
#     db="work_iclouds",
#     user="sa",
#     passwd="3361098@qq.com",
#     host="mysqldb",
#     port=3306,
# )


# query_string = "SELECT k,v FROM test WHERE k='%s'" % ("k1",)
# cp.runQuery(query_string).addCallback(printResult).addErrback(printError)


# def printResult(result):
#     if result:
#         print("%s's value is : %i" % (result[0][0], result[0][1]))
#     else:
#         print("No such key")


# def printError(error):
#     print(error)


# def UserLogin(*args, **kwargs):
#     """用户登录"""
#     sql = 'select id from Tank_Username_List where username="%s" and password="%s"' % (
#         kwargs["username"],
#         kwargs["password"],
#     )
#     dbpool.runQuery(sql).addCallback(printResult).addErrback(printError)

# =======================================================================================
import pymysql


class mainMySQLdb:
    def __init__(self):
        self.DB = pymysql.connect(
            database="work_iclouds",
            user="sa",
            passwd="3361098@QQ.com",
            host="mysqldb",
            port=3306,
        )
        self.cursor = self.DB.cursor()

    def __del__(self):
        self.DB.close()

    def selectMDB(self, sql, only=1):
        """查询数据"""
        self.cursor.execute(sql)
        if only:
            data = self.cursor.fetchone()
        else:
            data = self.cursor.fetchall()
        return data

    def executeMDB(self, sql):
        """插入数据事务"""
        try:
            self.cursor.execute(sql)
            self.DB.commit()
        except:
            self.DB.rollback()

    # ----------------------------------------
    def UserLogin(self, user, pasd):
        """用户登录"""
        sql = (
            'select id from Tank_Username_List where username="%s" and password="%s"'
            % (
                user,
                pasd,
            )
        )
        data = self.selectMDB(sql)
        return data

    def RoleList(self, uid):
        """查询角色列表"""
        sql = "select * from Tank_Rolename_List where parent=%d" % (uid,)
        data = self.selectMDB(sql, 0)
        return data

    def RoleInfo(self, uid):
        """查询角色属性"""
        sql = "select * from Tank_Rolename_List where id=%d" % (uid,)
        data = self.selectMDB(sql)
        return data
